home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / io / ObjectStreamField.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  6.9 KB  |  282 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)ObjectStreamField.java    1.25 98/04/30
  3.  *
  4.  * Copyright 1996-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17. import java.lang.reflect.Field;
  18.  
  19. /**
  20.  * A description of a Serializable field from a Serializable class.
  21.  * An array of ObjectStreamFields is used to declare the Serializable
  22.  * fields of a class.
  23.  *
  24.  * @author  Roger Riggs
  25.  * @version 1.25, 04/30/98
  26.  * @see ObjectStreamClass
  27.  */
  28. public class ObjectStreamField implements Comparable {
  29.     /**
  30.      * Create a Serializable field with the specified type.
  31.      * This field should be documented with a <code>serialField</code>
  32.      * tag. 
  33.      */
  34.     public ObjectStreamField(String n, Class clazz) {
  35.         name = n;
  36.         this.clazz = clazz;
  37.  
  38.     // Compute the typecode for easy switching
  39.     if (clazz.isPrimitive()) {
  40.         if (clazz == Integer.TYPE) {
  41.         type = 'I';
  42.         } else if (clazz == Byte.TYPE) {
  43.         type = 'B';
  44.         } else if (clazz == Long.TYPE) {
  45.         type = 'J';
  46.         } else if (clazz == Float.TYPE) {
  47.         type = 'F';
  48.         } else if (clazz == Double.TYPE) {
  49.         type = 'D';
  50.         } else if (clazz == Short.TYPE) {
  51.         type = 'S';
  52.         } else if (clazz == Character.TYPE) {
  53.         type = 'C';
  54.         } else if (clazz == Boolean.TYPE) {
  55.         type = 'Z';
  56.         }
  57.     } else if (clazz.isArray()) {
  58.         type = '[';
  59.         typeString = ObjectStreamClass.getSignature(clazz).intern();
  60.     } else {
  61.         type = 'L';
  62.         typeString = ObjectStreamClass.getSignature(clazz).intern();
  63.     }
  64.     }
  65.  
  66.     /**
  67.      * Create a default Serializable field for <code>field</code>.
  68.      */
  69.     ObjectStreamField(Field field) {
  70.     this(field.getName(), field.getType());
  71.     this.field = field;
  72.     }
  73.  
  74.     /**
  75.      * Create an ObjectStreamField containing a reflected Field.
  76.      */
  77.     ObjectStreamField(String n, char t, Field f, String ts)
  78.     {
  79.     //    System.out.println("new field, " + n + " " + t + " " + ts);
  80.     name = n;
  81.     type = t;
  82.     field = f;
  83.     typeString = (ts != null ? ts.intern() : null);
  84.     }
  85.  
  86.     /**
  87.      * SearchKey constructor.
  88.      * @see #compareTo(Object)
  89.      */
  90.     private ObjectStreamField(String name, boolean isPrimitive) {
  91.  
  92.     // only set fields that compareTo uses for comparison.
  93.     this.name = name;
  94.     setSearchKeyTypeString(isPrimitive);
  95.     }
  96.  
  97.     /**
  98.      * Get the name of this field.
  99.      */
  100.     public String getName() {
  101.         return name;
  102.     }
  103.  
  104.     /**
  105.      * Get the type of the field.
  106.      */
  107.     public Class getType() {
  108.         if (clazz != null)
  109.             return clazz;
  110.     switch (type) {
  111.     case 'B': clazz = Byte.TYPE;
  112.         break;
  113.     case 'C': clazz = Character.TYPE;
  114.         break;
  115.     case 'S': clazz = Short.TYPE;
  116.         break;
  117.     case 'I': clazz = Integer.TYPE;
  118.         break;
  119.     case 'J': clazz = Long.TYPE;
  120.         break;
  121.     case 'F': clazz = Float.TYPE;
  122.         break;
  123.     case 'D': clazz = Double.TYPE;
  124.         break;
  125.     case 'Z': clazz = Boolean.TYPE;
  126.         break;
  127.     case '[':
  128.     case 'L':
  129.         clazz = Object.class;
  130.         break;
  131.     }
  132.  
  133.         return clazz;
  134.     }
  135.  
  136.     /** 
  137.      * Returns character encoding of field type.
  138.      * The encoding is as follows:
  139.      * <blockquote><pre>
  140.      * B            byte
  141.      * C            char
  142.      * D            double
  143.      * F            float
  144.      * I            int
  145.      * J            long
  146.      * L            class or interface
  147.      * S            short
  148.      * Z            boolean
  149.      * [            array
  150.      * </pre></blockquote>
  151.      */
  152.     public char getTypeCode() {
  153.     return type;
  154.     }
  155.  
  156.     /**
  157.      * Return the JVM type signature.
  158.      *
  159.      * @returns null if this field has a primitive type.
  160.      */
  161.     public String getTypeString() {
  162.     return typeString;
  163.     }
  164.  
  165.     /**
  166.      * Offset of field within instance data.
  167.      */
  168.     public int getOffset() {
  169.     return bufoffset;
  170.     }
  171.  
  172.     /** 
  173.      * Offset within instance data.
  174.      */
  175.     protected void setOffset(int offset) {
  176.     bufoffset = offset;
  177.     }
  178.  
  179.     /*
  180.      * Default constructor creates an empty field.
  181.      * Usually used just to get to the sort functions.
  182.      */ 
  183.     ObjectStreamField() {
  184.     }
  185.  
  186.     /**
  187.      * Return true if this field has a primitive type.
  188.      */
  189.     public boolean isPrimitive() {
  190.     return (type != '[' && type != 'L');
  191.     }
  192.  
  193.     /**
  194.      * Compare this field with another <code>ObjectStreamField</code>.
  195.      * Return -1 if this is smaller, 0 if equal, 1 if greater.
  196.      * Types that are primitives are "smaller" than object types.
  197.      * If equal, the field names are compared.
  198.      */
  199.     public int compareTo(Object o) {
  200.     ObjectStreamField f2 = (ObjectStreamField)o;
  201.     boolean thisprim = (this.typeString == null);
  202.     boolean otherprim = (f2.typeString == null);
  203.  
  204.     if (thisprim != otherprim) {
  205.         return (thisprim ? -1 : 1);
  206.     }
  207.     return this.name.compareTo(f2.name);
  208.     }
  209.  
  210.     /**
  211.      * Return a string that describes this field.
  212.      */
  213.     public String toString() {
  214.     if (typeString != null)
  215.         return typeString + " " + name;
  216.     else
  217.         return type + " " + name;
  218.     }
  219.  
  220.     /**
  221.      * Compare the type of this ObjectStreamField with <code>other</code>.
  222.      * 
  223.      * @returns true if both ObjectStreamFields are serializable compatible.
  224.      */
  225.     boolean typeEquals(ObjectStreamField other) {
  226.     if (other == null || type != other.type)
  227.       return false;
  228.  
  229.     /* Optimization: Since interning typeString, 
  230.      *               this short circuit can save some time.
  231.      * Also, covers case where both typeStrings are null. 
  232.      */
  233.     if (typeString == other.typeString)
  234.         return true;
  235.     else
  236.         return ObjectStreamClass.compareClassNames(typeString,
  237.                                other.typeString,
  238.                                '/');
  239.     }
  240.  
  241.     /*
  242.      * Return a field.
  243.      * @see java.lang.reflect.Field
  244.      */
  245.     Field getField() {
  246.      return field;
  247.     }
  248.  
  249.     void setField(Field field) {
  250.      this.field = field;
  251.     }
  252.  
  253.  
  254.     /**
  255.      * @returns a ObjectStreamField with enough fields set to search 
  256.      *          a list for a matching ObjectStreamField.
  257.      * @see #compareTo(Object)
  258.      */
  259.     static ObjectStreamField constructSearchKey(String fieldName, 
  260.                         Class fieldType) 
  261.     {
  262.     return new ObjectStreamField(fieldName, fieldType.isPrimitive());
  263.     }
  264.  
  265.     void setSearchKeyTypeString(boolean isPrimitive) {
  266.     typeString = isPrimitive ? null : OBJECT_TYPESTRING;
  267.     }
  268.  
  269.     private String name;    // the name of the field
  270.     private char type;        // first byte of the type signature
  271.     private Field field;    // Reflected field.
  272.     private String typeString;    // iff object, fully qualified typename containing '/'
  273.     private int bufoffset;      // offset in the data buffer, or index in objects
  274.     private Class clazz;    // the type of this field, if has been resolved
  275.  
  276.     /* a string object used for ObjectStreamField search. For search purposes,
  277.      * it is sufficient that this string is non-null.
  278.      */
  279.     private static final String OBJECT_TYPESTRING=new String("");
  280.  
  281. };
  282.